package pCloud.client;

import java.io.IOException;

import pCloud.service.ProtocolHandle;
import pCloud.transport.Communicator;
import pCloud.transport.EndPointTransportHandle;
import pCloud.transport.TransportHandle;

public abstract class Connector {
	EndPointTransportHandle transportHandler;
	public ProtocolHandle protocolHandler;

	String remoteHost;
	int port;
	
	Communicator communicator;

	public Connector(String remoteHost, int port, Communicator comm) throws Exception {
		this.remoteHost = remoteHost;
		this.port = port;
		this.communicator = comm;
		this.init();
	}

	private void init() throws Exception {
		this.transportHandler = this.getNewClientTransportHandle(
				this.remoteHost, this.port);
	}

	public void connect() throws Exception {
		TransportHandle conn = this.transportHandler.connect();
		this.protocolHandler = this.getNewProtocolHandle();
		protocolHandler.init(conn, this.communicator);
		protocolHandler.startReceiverThread();
	}

	public abstract EndPointTransportHandle getNewClientTransportHandle(
			String host, int port) throws Exception;

	public abstract ProtocolHandle getNewProtocolHandle() throws Exception;
}
